package com.jsyh.maidu.presenter;

import android.content.Context;

import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.config.SPConfig;
import com.jsyh.maidu.http.BaseDelegate;
import com.jsyh.maidu.http.OkHttpClientManager;
import com.jsyh.maidu.model.BaseModel;
import com.jsyh.maidu.model.CartGoodsModel;
import com.jsyh.maidu.model.CollectGModel;
import com.jsyh.maidu.model.ShopModel;
import com.jsyh.maidu.utils.SPUtils;
import com.jsyh.maidu.views.CollectGoodsView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Su on 2015/10/20.
 */
public class CollectGoodsPresenter extends BasePresenter {

    private CollectGoodsView mCollectGoodsView;

    private Context mContext;


    public CollectGoodsPresenter(Context mContext) {
        this.mContext = mContext;
        this.mCollectGoodsView = (CollectGoodsView) mContext;
    }


    /**
     * 关注商品列表
     */
    public void getCollectList() {
        Map<String, String> params = getDefaultMD5Params("goods", "collectlist");
        String key = (String) SPUtils.get(mContext, "key", "");

        params.put(SPConfig.KEY, key);

        OkHttpClientManager.postAsyn(mContext, ConfigValue.CollectGoodsList, params,
                new BaseDelegate.ResultCallback<CollectGModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {

                    }

                    @Override
                    public void onResponse(CollectGModel response, Object tag) {
                        mCollectGoodsView.getCollectList(response);
                    }
                });
    }

    /**
     * 关注店铺列表
     */
    public void getCollectShop(Context context, String key, String page){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> param = new HashMap<>();
        param.put("key", key);
        param.put("page", page);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "follow_shop/all", param, new BaseDelegate.ResultCallback<ShopModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(ShopModel response, Object tag) {
                mLoadingDialog.dismiss();
                mCollectGoodsView.getShopList(response);
            }
        });
    }

    public void deleteCollectShop(Context context, String key, String id){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> param = new HashMap<>();
        param.put("key", key);
        param.put("id", id);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "follow_shop/delete", param, new BaseDelegate.ResultCallback<BaseModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(BaseModel response, Object tag) {
                mLoadingDialog.dismiss();
                mCollectGoodsView.deleteShopResult(response);
            }
        });
    }

}
